package fun.abbas.demo240621.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.function.IntConsumer;
import java.util.function.ToIntFunction;

public class BPView2 extends View {

    private int width;
    private int height;

    private int paddingTopBottom;
    private int paddingStartEnd;
    private List<Integer> data;
    private int minData;
    private int maxData;
    private int strokeWidth;
    private final Paint linePaint = new Paint();
    private LinearGradient linearGradient;
    private final int[] colors = new int[]{Color.parseColor("#A4D86F"), Color.parseColor("#99CC5B")};

    public BPView2(Context context) {
        super(context);
        init();
    }

    public BPView2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BPView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public BPView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init() {
        strokeWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getContext().getResources().getDisplayMetrics());
        linePaint.setColor(Color.BLUE);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeCap(Paint.Cap.ROUND);
        linePaint.setStrokeWidth(strokeWidth);

        setData(Arrays.asList(99, 98, 99, 99, 97, 99, 99, 97, 99, 98, 98, 99));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        paddingTopBottom = (int) (height * 0.1f);
        paddingStartEnd = (int) (width * 0.1f);
        linearGradient = new LinearGradient(0, paddingTopBottom, 0, heightMeasureSpec - paddingTopBottom, colors, null, Shader.TileMode.CLAMP);
        linePaint.setShader(linearGradient);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        int actWidth = width - paddingStartEnd * 2;
        int actHeight = height - paddingTopBottom * 2;
        float intervalWidthSpace = (float) actWidth / data.size();
        int intervalHeightSpace = actHeight / (maxData - minData+1);
        int lineY = height / 2;
        for (int i = 0; i < data.size(); i++) {
            Integer j = data.get(i);
            int size = j - minData + 1;
            float x = paddingStartEnd + i * intervalWidthSpace + strokeWidth;
            int y = size * intervalHeightSpace / 2;
            canvas.drawLine(x,
                    lineY - y,
                    x,
                    lineY + y,
                    linePaint);
        }
    }

    private void setData(List<Integer> data) {
        this.data = data;
        data.stream()
                .mapToInt(i -> i)
                .min().ifPresent(i -> minData = i);
        data.stream()
                .mapToInt(i -> i)
                .max()
                .ifPresent(i -> maxData = i);
        invalidate();
    }

}
